package com.example.demo;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.widget.Toast;

import com.actionbarsherlock.ActionBarSherlock;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

public class MainActivity extends SherlockFragmentActivity implements
		OnGestureListener {

	private GestureDetector detector;
	ActionBarSherlock mSherlock = ActionBarSherlock.wrap(this);
	int mSortMode = -1;
	int pageCount = 0;

	public static int THEME = R.style.Theme_Sherlock;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		FirstFragment first = new FirstFragment();
		ft.add(R.id.container, first);
		ft.commit();
		pageCount = 1;
		detector = new GestureDetector(this);
		mSherlock
				.setUiOptions(ActivityInfo.UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW);

		ActionBar actionBar = getSupportActionBar();
		// 将ActionBar的操作模型设置为NAVIGATION_MODE_LIST
		actionBar.setLogo(R.drawable.biz_pics_main_back_normal);
		actionBar.setTitle("网易图片");
	}

	public void onSort(MenuItem item) {
		mSortMode = item.getItemId();
		// Request a call to onPrepareOptionsMenu so we can change the sort icon
		invalidateOptionsMenu();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return this.detector.onTouchEvent(event);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.main_options, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		if (e1.getX() - e2.getX() > 120) {
			if (pageCount == 1) {

				FragmentTransaction ft = getSupportFragmentManager()
						.beginTransaction();
				ft.setCustomAnimations(R.animator.slide_out_right,
						R.animator.slide_out_left);
				SecondFragment second = new SecondFragment();
				ft.replace(R.id.container, second);
				ft.commit();
				pageCount = 2;
			}else {
				Toast.makeText(MainActivity.this, "最后一页。", Toast.LENGTH_LONG).show();
			}
			return true;
		} else if (e1.getX() - e2.getX() < -120) {
			// FragmentTransaction ft =
			// getSupportFragmentManager().beginTransaction();
			// ft.setCustomAnimations(R.animator.slide_in_left,
			// R.animator.slide_out_right);
			// SecondFragment second = new SecondFragment();
			// ft.replace(R.id.container, second);
			// ft.commit();
			Intent intent = new Intent();
			intent.setClass(MainActivity.this, MainFragmentActivity.class);
			startActivity(intent);
			overridePendingTransition(R.animator.slide_in_left,
					R.animator.slide_out_right);// 在跳转的语句后加上这条语句。
			return true;
		}
		return true;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

}
